package com.chen.webuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chen.exception.BusinessException;
import com.chen.util.RedisKey;
import com.chen.util.ResultCode;
import com.chen.webuser.entity.WebUser;
import com.chen.webuser.entity.WebUserInfo;
import com.chen.webuser.entity.dto.LoginDto;
import com.chen.webuser.service.LoginService;
import com.chen.webuser.service.WebUserInfoService;
import com.chen.webuser.service.WebUserLogService;
import com.chen.webuser.service.WebUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private WebUserService webUserService;
    @Autowired
    private WebUserInfoService webUserInfoService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private WebUserLogService webUserLogService;

    /**
     * 用户注册
     *
     * @param webUser
     */
    @Transactional  //操作两张表加事务
    @Override
    public void register(WebUser webUser) {
        // 保存用户信息到web_user
        //封装注册时间
        webUser.setRegisterTime(LocalDateTime.now());
        webUserService.save(webUser);
        //存入成功返回用户id到webUser中
        Integer uid = webUser.getUid();

        //封装webUserInfo
        WebUserInfo webUserInfo = webUser.getWebUserInfo();
        webUserInfo.setUid(uid);

        String headPortrait = "https://raw.githubusercontent.com/chengj666/picgo-images/master/202510191921545.png";
        webUserInfo.setHeadPortrait(headPortrait);

        //保存webUserInfo
        webUserInfoService.save(webUserInfo);

    }

    /**
     * 登录
     *
     * @param loginDto
     * @return
     */
    @Override
    public WebUser login(LoginDto loginDto) {
        //先去redis中查询,看看缓存里面有没有
        String key = String.format(RedisKey.userUsernameKey, loginDto.getUsername());
        Object o = redisTemplate.opsForValue().get(key);
        if (o != null) {
            // 说明查到了
            return (WebUser)o;
        } else {
            //没有查询到
            // 根据用户名去查询用户
            LambdaQueryWrapper<WebUser> wp = new LambdaQueryWrapper<>();
            wp.eq(WebUser::getUsername, loginDto.getUsername());
            WebUser sqlUser = webUserService.getOne(wp);
            if (sqlUser == null) {
                // 用户不存在
                throw new BusinessException(ResultCode.USER_NOT_EXIST);
            }
            if (!sqlUser.getPassword().equals(loginDto.getPassword())) {
                throw new BusinessException(ResultCode.LOGIN_ERROR);
            }


            //封装 UserInfo
            WebUserInfo userinfo = webUserInfoService.getById(sqlUser.getUid());
            sqlUser.setWebUserInfo(userinfo);

            //开启多线程记录日志和存储redis
            new Thread(() -> {
                // 登录成功 存储到redis
                redisTemplate.opsForValue().set(key, sqlUser);

                // 登录成功 记录日志
                webUserLogService.saveLog(sqlUser);

            }).start();
            return sqlUser;
        }

    }

}
